<?php


namespace app\api\controller;
use app\admin\model\UserAddress as UA;
use think\Db;
use think\Request;

class UserAddress extends Base
{
    /***
     * @param Request $request
     * 获取用户全部地址
     * user_id 用户id
     */
    public function getUserAddress(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) ){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        $address = new UA();
        $where['user_id'] = $data['user_id'];
        $rs = $address->getAll($where);
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
            $res['data']['msg'] = '获取成功';
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '获取失败';
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    /***
     * @param Request $request
     * 获取用户常用地址
     */
    public function getUserDefaultAddress(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) ){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $address = new UA();
        $where['user_id'] = $data['user_id'];
        $where['default'] = 1;
        $rs = $address->getOne($where);
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
            $res['data']['msg'] = '获取成功';

        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '获取失败';
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    /***
     * @param Request $request
     * 添加地址
     */
    public function addAddress(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['name']) or !isset($data['phone']) or !isset($data['city']) or !isset($data['address'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $db = new UA();
        $addData['user_id'] = $data['user_id'];
        $addData['name'] = $data['name'];
        $addData['phone'] = $data['phone'];
        $addData['city'] = $data['city'];
        $addData['address'] = $data['address'];
        //是否有常用地址，无则default为1
        $where['default'] = 1;
        $where['user_id'] = $data['user_id'];
        $addData['default'] = !$db->getOne($where)?1:0;

        $rs = $db->add($addData);
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '添加成功';
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '添加失败';
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

    /***
     * @param Request $request
     *修改地址
     * user_id
     * address_id
     */
    public function editAddress(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['address_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $db = new UA();
        //查询是否存在
        $where['user_id'] = $data['user_id'];
        $where['id'] = $data['address_id'];
        if(!$db->getOne($where)){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '该信息不存在';
        }else{
            unset($data['user_id']);
            unset($data['address_id']);
            $editData = $data;
            if($db->edit($where,$editData)){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '修改成功';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '修改失败';
            }
            $res['status'] = 200;
            echo json_encode($res);
        }
    }

    /***
     * @param Request $request
     * 修改常用地址
     * user_id 用户id
     * address_id 新地址id
     */
    public function changeDefaultAddress(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['address_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $db = new UA();
        $where['user_id'] = $data['user_id'];
        $where['id'] = $data['address_id'];
        //基础信息是否存在
        if(!$db->getOne($where)){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '该信息不存在';
        }else{
            //是否已有常用地址
            $where['default'] = 1;
            unset($where['id']);
            $info = $db->getOne($where);
            if(!$info){
                unset($where['default']);
                $where['id'] = $data['address_id'];
                $editData['default'] = 1;
                if($db->edit($where,$editData)){
                    $res['data']['code'] = 1;
                    $res['data']['msg'] = '修改成功';
                }else{
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '修改失败';
                }
            }else{
                //有常用地址
                $bool = true;
                Db::startTrans();
                try{
                    //修改之前为0
                    $infoWhere['user_id'] = $info['user_id'];
                    $infoWhere['id'] = $info['id'];
                    $editData['default'] = 0;
                    Db::table('app_user_address')->where($infoWhere)->update($editData);
                    unset($where['default']);
                    //修改新的为1
                    $where['id'] = $data['address_id'];
                    $editData['default'] = 1;
                    Db::table('app_user_address')->where($where)->update($editData);
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    $bool = false;
                }
                if($bool){
                    $res['data']['code'] = 1;
                    $res['data']['msg'] = '修改成功';
                }else{
                    $res['data']['code'] = 0;
                    $res['data']['msg'] = '修改失败';
                }
            }
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    /***
     * @param Request $request
     * user_id 用户id
     * address_id 地址id
     */
    public function delUserAddress(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['address_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $db = new UA;
        $where['user_id'] = $data['user_id'];
        $where['id'] = $data['address_id'];
        if($db->del($where)){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '删除成功';
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '删除失败';
        }
        $res['status'] = 200;
        echo json_encode($res);
    }
}